#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "featurepp.h"

/* arguments:
   argv[1] inputfile (linesformat)
   argv[2] outputfile (csv)
   argv[3] desired dimension
   
   note: if dimension does not fit, file will be stuffed with zeroes at the last
   instance.
   */
main(int argc, char *argv[]) {
   
   int c, i, d = 0;
   
   FILE *fpsource, *fpoutput;

   char t[1000];                    // Buffer
   char zero[] = "0";

   if (argc!=4) {
   	printf("usage: lines2csv linesinputfile csvoutputfile dimension\n");
   	exit(1);
   }

   printf("Parsing lines to comma-seperated file-Format...\n");   

   printf("Open: %s\n", argv[1]);
   if ((fpsource = fopen(argv[1], "r"))==NULL) {
      printf("error: can't open Sourcefile %s\n", argv[1]);
      exit(1);
   }

   printf("Open: %s\n", argv[2]);
   if ((fpoutput = fopen(argv[2], "w"))==NULL) {
      printf("error: can't open Sourcefile %s\n", argv[2]);
      exit(1);
   }

   d = atoi(argv[3]);

   while (c != EOF) {
   	for (i = 0; i < (d-1); i++) {
           if (c = readArg(t, fpsource)!=EOF) {
               writeArg(t, fpoutput, ',');
           } else {
               writeArg(zero, fpoutput, ',');
           }
       }
       if ((c = readArg(t, fpsource))!=EOF) {
           writeArg(t, fpoutput, '\n');
       } else {
           writeArg(zero, fpoutput, '\n');
       }
   }

   printf("done.\n");
   fclose(fpsource);
   fclose(fpoutput);
   
}
